var activityID = window.location.toString().split('/')[5];
var dt = $('.dataTablesAjax');
var ajax_datatable = [];

/**
 * 打开二维码模态框
 */
function showQrcodeModal(that) {
    that = $(that);
    var $url = that.attr('data-url');
    var $img = that.attr('data-img');
    var $type = that.attr('data-type');
    var $modal =$('#offline-qrcode-modal');
    $modal.find('.modal-title').text('线下派票——'+$type+'二维码');
    $modal.find('.qrcode-url').text('二维码对应链接：'+$url);
    $modal.find('.qrcode-img').attr('src',$img);
    $modal.modal('show');
}

$(function () {
    $('.date-input').datetimepicker({
        format: 'yyyy-mm-dd hh:ii'
    });
    //线上抢票异步修改时间等
    $('.setting input').change(function () {
        var _this = $(this);
        var $way = _this.parents('.setting').attr('data-way');
        console.log($way);
        var $value = _this.val();
        if(_this.attr('type')=='checkbox'){
            $value = _this.prop('checked');
        }
        $.post('/admin/eticket/'+activityID+'/ticket/ajaxSetting/'+$way,{
            key:_this.attr('id'),
            value:$value
        },function (data) {
            if(data.status==0) {
                toastr.success('修改成功');
            }else{
                toastr.success(data.error);
            }
        });
    });

    var dts = $('.dataTablesAjax');
    dts.each(function (i, dt) {
        dt = $(dt);
        var $way = dt.attr('data-way');
        ajax_datatable[$way] = dt.DataTable({
            "processing": true,
            "serverSide": true,
            "searching": false,
            "searchDelay": 800,
            "search": {
                "regex": true
            },
            "ajax": {
                'url': '/admin/eticket/' + activityID + '/ticket/stat/' + $way
            },
            "pagingType": "full_numbers",
            "orderCellsTop": true,
            "dom": '<"html5buttons"B>lTfgitp',
            "buttons": [
                {extend: 'copy', title: 'permission'},
                {extend: 'csv', title: 'permission'},
                {extend: 'excel', title: 'permission'},
                {extend: 'pdf', title: 'permission'},
                {
                    extend: 'print',
                    customize: function (win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');
                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ],
            "columns": [
                {
                    "data": "type",
                    "name": "type",
                    "orderable": true
                },
                {
                    "data": "total",
                    "name": "total",
                    "orderable": true
                },
                {
                    "data": "left",
                    "name": "left",
                    "orderable": true
                },
                {
                    "data": "actionButton",
                    "name": "actionButton",
                    "type": "html",
                    "orderable": false
                }
            ],
            "drawCallback": function (settings) {
                ajax_datatable[$way].$('.tooltips').tooltip({
                    placement: 'top',
                    html: true
                });
            },
            "language": {
                url: '/admin/i18n'
            }
        });
    });

    var addTicketsOkBtn = $('.add-tickets-ok-btn');
    addTicketsOkBtn.click(function () {
        var $form = $(this).parent().prev().children('form');
        var $modal = $(this).parents('.add-tickets-modal');
        var $way = $(this).attr('data-way');
        $(this).attr('disabled', 'disabled');
        $(this).text('正在添加，请稍后...');
        $.ajax({
            url: '/admin/eticket/' + activityID + '/ticket/batchStore/' + $way,
            type: 'POST',
            cache: false,
            data: new FormData($form[0]),
            processData: false,
            contentType: false
        }).done(function (res) {
            toastr.success('添加成功！');
            $modal.modal('hide');
            addTicketsOkBtn.attr('disabled', false);
            addTicketsOkBtn.text('确定');
            ajax_datatable[$way].ajax.reload();
            console.log(res);
        }).fail(function (res) {
            toastr.error('添加出错，请刷新重试！');
        });
    });

    $('#billing-btn').click(function () {
        $(this).attr('disabled', 'disabled');
        $(this).text('正在开票，请稍后...');
        $.get('/admin/eticket/' + activityID + '/billing',function (data) {
            toastr.success('共开票：'+data.total+'个<br>'+
                '成功获取门票：'+data.success+'个<br>'+
                '未能获取门票：'+data.fail+'个<br>'
            );
            addTicketsOkBtn.attr('disabled', false);
            addTicketsOkBtn.text('手动开票');
        });
    });
});
